<?php

	if( !isset($_COOKIE["username"]) ){
		header("Location:login.php");
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}

	include_once 'json_function.php';
	$ini_array = parse_ini_file("config/config.ini");

	$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
	mysqli_set_charset($conn, "utf8");
	ini_set('default_charset', "UTF-8");
	date_default_timezone_set($ini_array["default_timezone"]);

	//echo "fdsf";
	$type = $_POST["type"];
	if( $type == 0 ){
		$getAppointmentSql = "select appointment_no, First_name, Surname,  appointment_datetime, appointment.type, appointment.sub_type, patient.patient_no, patient.`date_last_ak`, patient.`date_last_sl`, patient.`landline`, patient.mobile, patient.Date_birth, appointment.isFirstVisit from appointment, patient where appointment.Patient_no=patient.Patient_no and appointment.type=1  order by appointment_datetime desc";
	}else{
		$getAppointmentSql = "select appointment_no, First_name, Surname,  appointment_datetime, appointment.type, appointment.sub_type, patient.patient_no, patient.`date_last_ak`, patient.`date_last_sl`, patient.`landline`, patient.mobile, patient.Date_birth, appointment.isFirstVisit from appointment, patient where appointment.Patient_no=patient.Patient_no and appointment.type=1 and appointment.sub_type=".$type."  order by appointment_datetime desc";
	}
	$appointmentResult = mysqli_query($conn, $getAppointmentSql);
	//echo mysql_error();
	$data = "{\"date\":\"\",\"data\":[";
	$count = 0;
	while( $row = mysqli_fetch_array($appointmentResult) ){
		if( $count != 0 ){
			$data .= ",";
		}else{
			$count = 1;
		}
		$data .= "{\"appointmentNo\":\"".$row["appointment_no"]."\",";
		$data .= "\"patientName\":\"".replaceQuoteAndSlash($row["First_name"]."  ".$row["Surname"])."\",";
		$data .= "\"doctorName\":\"Geraldine\",";
		$data .= "\"mobile\":\"".$row["mobile"]."\",";
		$data .= "\"dateOfBirth\":\"".date("d-m-Y", strtotime($row["Date_birth"]))."\",";
		if( strcmp($row["appointment_datetime"], "") == 0 ){
			$data .= "\"appointmentDate\":\"\",";
			$data .= "\"appointmentTime\":\"\",";
		}else{
			$data .= "\"appointmentDate\":\"".date("d-m-Y",strtotime($row["appointment_datetime"]))."\",";
			$data .= "\"appointmentTime\":\"".date("H:i",strtotime($row["appointment_datetime"]))."\",";
		}
		$data .= "\"type\":\"".$row["type"]."\",";
		$data .= "\"sub_type\":\"".$row["sub_type"]."\",";
		$data .= "\"patientNo\":\"".$row["patient_no"]."\",";
		
		if( strcmp($row["date_last_ak"], "") == 0 ){
			$data .= "\"dateLastAK\":\"\",";
		}else{
			$data .= "\"dateLastAK\":\"".date("d-m-Y",strtotime($row["date_last_ak"]))."\",";
		}
		
		if( strcmp($row["date_last_sl"], "") == 0 ){
			$data .= "\"dateLastSL\":\"\",";
		}else{
			$data .= "\"dateLastSL\":\"".date("d-m-Y",strtotime($row["date_last_sl"]))."\",";
		}
		$data .= "\"isFirstVisit\":\"".$row["isFirstVisit"]."\",";
		$data .= "\"landline\":\"".$row["landline"]."\"}";
	}
	$data .= "]}";
	echo JsonCharFilter($data);
	
?>